﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Silverlight.Testing;
using System8ToyMC;
using System.Windows.Controls;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;

namespace t_System8ToyMCControls
{
    /// <summary>
    /// Test out the load from text dialog box!
    /// </summary>
    [TestClass]
    public class t_LoadFromTextDialog : PresentationTest
    {
        LoadFromTextDialog _val;
        TextBox _tb;
        Button _cvt, _can;

        [TestInitialize]
        public void CreateNVSetup()
        {
            _val = new LoadFromTextDialog();
            TestPanel.Children.Add(_val);
            _tb = _val.FindName("TBConvertText") as TextBox;
            _cvt = _val.FindName("BConvert") as Button;
            _can = _val.FindName("BCancel") as Button;
        }

        [TestMethod]
        public void TestCTor()
        {
            Assert.IsNotNull(_val, "Didn't create the object");
            Assert.IsNotNull(_tb, "COuldn't find main text box");
            Assert.IsNotNull(_cvt, "Couldn't find the convert button");
            Assert.IsNotNull(_can, "Couldn't find the cancel button");
        }

        [TestMethod]
        public void TestTextBinding()
        {
            _tb.Text = "hi there";
            Assert.AreEqual("hi there", _val.Text, "Expected text to have been properly updated");

            _val.Text = "7777";
            Assert.AreEqual("7777", _tb.Text, "Setting the value does not update the actual text!");
        }

        [TestMethod]
        [Asynchronous]
        [Timeout(1000)]
        public void TestCancelEvent()
        {
            _val.CancelConvert += (o, args) => TestComplete();

            ButtonAutomationPeer bp = new ButtonAutomationPeer(_can);
            IInvokeProvider bi = (IInvokeProvider)bp;
            EnqueueCallback(() => bi.Invoke());
        }

        [TestMethod]
        [Asynchronous]
        [Timeout(1000)]
        public void TestConvertEvent()
        {
            _val.Convert += (o, a) => TestComplete();

            _tb.Text = "hi there";
            ButtonAutomationPeer bp = new ButtonAutomationPeer(_cvt);
            IInvokeProvider bi = (IInvokeProvider)bp;
            EnqueueCallback(() => bi.Invoke());
        }

        [TestMethod]
        [Asynchronous]
        [Timeout(1000)]
        public void TestConvertEnabled()
        {
            Assert.IsFalse(_cvt.IsEnabled, "The convert button should not be enabled!");
            TextBoxAutomationPeer tbp = new TextBoxAutomationPeer(_tb);
            IValueProvider val = (IValueProvider) tbp;
            EnqueueCallback(() => val.SetValue("dude"));
            EnqueueConditional(() => _cvt.IsEnabled);
            EnqueueTestComplete();
        }
    }
}